<?php
namespace app\api\service;



use think\Exception;

class HaiDing{
    public function __construct(){

        $this->site_url = "http://cre.superbrandmall.com:8280";  //请求地址 （需要拼接上版本号）
//        $this->site_url = "https://cre2-test.superbrandmall.com";  //请求地址 （需要拼接上版本号）
        $this->merchant_url = "/cre-agency-server/rest/1/tenant/query";  //请求地址 （需要拼接上版本号）
        $this->contract_url = "/cre-agency-server/rest/investment/contract/queryContracts";  //请求地址 （需要拼接上版本号）
        $this->cost_url = "/cre-agency-server/rest/1/statement/queryByFilter";  //请求地址 （需要拼接上版本号）
        $this->pay_url = "/cre-agency-server/rest/1/payment/save";  //请求地址 （需要拼接上版本号）
        $this->deposit = "/cre-agency-server/rest/account/deposit/receipt/save";  //请求地址 （需要拼接上版本号）
        $this->brand = "/cre-agency-app-server/rest/1/brand/query";
    }

    /**
     * 获取品牌
     */
    public function getBrand($page,$limit,$keywords){

        $data['pageSize'] = $limit;
        $data['page'] = $page;
        $data['keyword'] = $keywords;
        $data['states']=['using'];
        $url = $this->site_url.$this->brand;
        return $this->url($url,$data);
    }
    /**
     * 获取商户信息   cre-agency-server/rest/onlineleasing/tenant/query
     */
    public function getMerchant($page,$limit){
        $data['pageSize'] = $limit;
        $data['page'] = $page;
        $filter['state'] = 'using';
//        $filter['type'] = 'merchant';
//        $filter['modifyTime'] = '';
        $data['filter'] = $filter;
        $url = $this->site_url.$this->merchant_url;
        return $this->url($url,$data);
    }
    /**
     * 获取合同信息
     */
    public function getContentInfo($page,$limit,$merchant_name='',$merchant_code='',$bunk_code='',$telephone='',$contract='',$code=''){
        $data['pageSize'] = $limit;
        $data['page'] = $page;
        $data['merchant_name'] = $merchant_name;//租户名称
        $data['merchant_code'] = $merchant_code;//租户code
        $data['bunk_code'] = $bunk_code;//铺位号
        $data['contractno'] = $code;
        $data['contract'] = $contract;//合同名称
        $url = $this->site_url.$this->contract_url;
        return $this->url($url,$data);
    }
    /**
     * 查询账单信息
     */
    public function getCostUrl($contract_no='',$order_id='',$merchant_name='',$page=0,$pageSize=100,$billNumber=''){
        $data['pageSize'] = $pageSize;
        $data['page'] = $page;
        $data['contract_no'] = $contract_no;
        $data['billNumber'] = $billNumber;
        $data['order_id'] = $order_id;
        $data['merchant_name'] = $merchant_name;
        $url = $this->site_url.$this->cost_url;
        return $this->url($url,$data);
    }
    /**
     * 核销
     */
    public function payInfo($data){
        $url = $this->site_url.$this->pay_url;
        return $this->url($url,$data);
    }
    /**
     * 租金保证金
     */
    public function deposit($data){
        $url = $this->site_url.$this->deposit;
        return $this->url($url,$data);
    }
    /**
     * 请求
     */
    public function url($url,$data){
        try {
            $result = Request::httpRequest($url,'POST',json_encode($data));
            $result = json_decode($result,true);
            if(isset($result['success'])&&$result['success']==true){
                return isset($result['body']['records'])?$result['body']['records']:$result['body'];
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            throw new Exception($e->getMessage());
        }

    }

}